package com.ymatou.iapi.trading.sellerquery2.parameter.sp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ymatou.query.trading.api.parameter.args.GetSellerOrderListCountArguments;
import com.ymatou.query.trading.api.parameter.sp.I_Sp_GetSellerOrderListCount;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;

/**
 * sp_trd_GetSellerOrderListCount_Full_daddtime
 * @author songyefeng
 *
 */
public class Sp_GetSellerOrderListCount_Full_daddtime implements
		I_Sp_GetSellerOrderListCount {

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> execute(GetSellerOrderListCountArguments args) {
		DefaultWapper db = new DefaultWapper();
		StringBuilder sql = new StringBuilder();
		DataUtil.SQL.declareVarOfSp("b", sql, "salesRefundOrderOnly", args.getSalesRefundOrderOnly());
		DataUtil.SQL.declareVarOfSp("b", sql, "timeoutType", args.getTimeoutType());
		DataUtil.SQL.declareVarOfSp("ti", sql, "timeoutType1", args.getTimeoutType1());
		DataUtil.SQL.declareVarOfSp("ti", sql, "timeoutType2", args.getTimeoutType2());
		DataUtil.SQL.declareVarOfSp("ti", sql, "timeoutType3", args.getTimeoutType3());
		DataUtil.SQL.declareVarOfSp("dt", sql, "timeoutBegin1", args.getTimeoutBegin1());
		DataUtil.SQL.declareVarOfSp("dt", sql, "timeoutEnd1", args.getTimeoutEnd1());
		DataUtil.SQL.declareVarOfSp("dt", sql, "timeoutBegin2", args.getTimeoutBegin2());
		DataUtil.SQL.declareVarOfSp("dt", sql, "timeoutEnd2", args.getTimeoutEnd2());
		DataUtil.SQL.declareVarOfSp("dt", sql, "timeoutBegin3", args.getTimeoutBegin3());
		DataUtil.SQL.declareVarOfSp("dt", sql, "timeoutEnd3", args.getTimeoutEnd3());
		sql.append(" ;with t as(");
		sql.append(" select o.iOrderId, o.iTradingStatus, cast(1 as bit) as bPaidInFull,");
		sql.append(" ROW_NUMBER() over(partition by o.iOrderId order by o.iOrderId) as n");
		sql.append(" from Ymt_Orders o with (nolock,index = idx_Ymt_Orders_iBuyerId_dAddTime) ");
		sql.append(" inner loop join Ymt_OrderInfo(nolock) i on o.iOrderId = i.iOrderId");
		sql.append(" where o.iBuyerId = " + args.getSellerId());
		sql.append(" and o.dAddTime between '" + args.getBeginTime()
				+ "' and '" + args.getEndTime() + "'");
		if (!DataUtil.isNullOrEmpty(args.getOrderStatus())) {
			sql.append(" and o.iTradingStatus in "
					+ DataUtil.toInOfSql2(args.getOrderStatus()));
		}
		String timeoutType = " and ("
				+ " @timeoutType = 0 or("
				+ " @timeoutType1 = 0 or (@timeoutType1 = 1 and (o.dPaidTime > @timeoutBegin1 and o.dPaidTime <= @timeoutEnd1 and o.iTradingStatus = 2) or @timeoutType1 = 2 and (o.dPaidTime <= @timeoutEnd1 and o.iTradingStatus = 2))"
				+ " and @timeoutType2 = 0 or (@timeoutType2 = 1 and (o.dAcceptTime > @timeoutBegin2 and o.dPaidTime <= @timeoutEnd2 and o.iTradingStatus = 17) "
				+ " or @timeoutType2 = 2 and (o.dAcceptTime <= @timeoutEnd2 and o.iTradingStatus = 17))"
				+ " and @timeoutType3 = 0 or (@timeoutType3 = 1 and (isnull(o.dPostPaidTime,o.dPaidTime) > @timeoutBegin3 and isnull(o.dPostPaidTime,o.dPaidTime) <= @timeoutEnd3 and o.iTradingStatus = 17)"
				+ " or @timeoutType3 = 2 and (isnull(o.dPostPaidTime,o.dPaidTime) <= @timeoutEnd3 and o.iTradingStatus = 17))"
				+ "))";
		sql.append(timeoutType);
		if(!DataUtil.isNullOrEmpty(args.getCatalogStatus())){
			sql.append(" and i.iCatalogStatus in "+DataUtil.toInOfSql(args.getCatalogStatus()));
		}
		if(args.getDomesticDelivered() != null){
			sql.append(" and isnull(o.bDomesticDelivered, 0) = "+args.getDomesticDelivered());
		}
		if(args.getKeyword() != null){
			sql.append(" and (o.iOrderId like '%"+args.getKeyword()+"%' or o.sBuyerLoginId like '%"+args.getKeyword()+"%' or i.sTitle like '%"+args.getKeyword()+"%')");
		}
		sql.append(" and (@salesRefundOrderOnly = 0 or o.iSalesRefundStatus is not null)");
		sql.append(" ) select iTradingStatus, cast(1 as bit) as bPaidInFull, count(iOrderId) as num from t where n = 1 group by iTradingStatus");
		return db.executeSql(sql);
	}
	
		public static void main(String[] argss) {
			GetSellerOrderListCountArguments args = new GetSellerOrderListCountArguments();
			args.setSellerId(3383);
			args.setTimeoutType(0);
			args.setBeginTime("2016-06-01 11:21:56.953");
			args.setEndTime("2016-07-01 11:21:56.953");
			List<Integer> orderStatus = new ArrayList<Integer>();
			orderStatus.add(1);
			orderStatus.add(2);
			args.setOrderStatus(orderStatus);
			new Sp_GetSellerOrderListCount_Full_daddtime().execute(args);
		}

}
